/* drivers/video/sprdfb/lcd_gc9503_mipi.c
 *
 * Support for gc9503 mipi LCD device
 *
 * Copyright (C) 2010 Spreadtrum
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "../sprdfb_chip_common.h"
#include "../sprdfb_panel.h"
#include "../sprdfb.h"

//#define LCD_Delay(ms)  uDelay(ms*1000)
///#define printk printf

#define  LCD_DEBUG
#ifdef LCD_DEBUG
#define LCD_PRINT printk
#else
#define LCD_PRINT(...)
#endif

#define MAX_DATA   56

typedef struct LCM_Init_Code_tag {
	unsigned int tag;
	unsigned char data[MAX_DATA];
}LCM_Init_Code;

typedef struct LCM_force_cmd_code_tag{
	unsigned int datatype;
	LCM_Init_Code real_cmd_code;
}LCM_Force_Cmd_Code;

#define LCM_TAG_SHIFT 24
#define LCM_TAG_MASK  ((1 << 24) -1)
#define LCM_SEND(len) ((1 << LCM_TAG_SHIFT)| len)
#define LCM_SLEEP(ms) ((2 << LCM_TAG_SHIFT)| ms)
//#define ARRAY_SIZE(array) ( sizeof(array) / sizeof(array[0]))

#define LCM_TAG_SEND  (1<< 0)
#define LCM_TAG_SLEEP (1 << 1)

static LCM_Init_Code init_data[] = {
	{LCM_SEND(8), {6,  0,0xF0,0x55,0xAA,0x52,0x08,0x00}},
	{LCM_SEND(5), {3,  0,0xF6,0x5A,0x87}},
	{LCM_SEND(2), {0xc1,0x3f}},
	{LCM_SEND(2), {0xC2,0x0E}},
	{LCM_SEND(2), {0xC6,0xF8}},
	{LCM_SEND(6), {4,  0,0xFA,0x0F,0x0F,0x00}},
	{LCM_SEND(2), {0x71,0x48}},
	{LCM_SEND(2), {0x72,0x48}},
	{LCM_SEND(5), {3,  0,0x73,0x00,0x44}},
	{LCM_SEND(2), {0xA3,0xEE}},
	{LCM_SEND(6), {4,  0,0xFD,0x28,0x28,0x00}},
	{LCM_SEND(2), {0x97,0xEE}},
	{LCM_SEND(2), {0x83,0x93}},
	{LCM_SEND(2), {0x9A,0x9A}},
	{LCM_SEND(2), {0x9B,0xAD}},
	{LCM_SEND(5), {3,  0,0x82,0x7B,0x7B}},
	{LCM_SEND(2), {0xB1,0x10}},
	{LCM_SEND(5), {3,  0,0x7A,0x13,0x1A}},
	{LCM_SEND(5), {3,  0,0x7B,0x13,0x1A}},
//gatetiming
	{LCM_SEND(35), {33,  0,0x6D,0x1E,0x10,0x0E,0x0C,0x0A,0x1E,0x02,0x1E,0x19,0x1E,0x1A,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1A,0x1E,0x19,0x1E,0x01,0x1E,0x09,0x0B,0x0D,0x0F,0x1E}},
	{LCM_SEND(19), {17,  0,0x64,0x18,0x06,0x03,0x21,0x70,0x03,0x18,0x05,0x03,0x22,0x70,0x03,0x10,0x73,0x10,0x73}},
	{LCM_SEND(19), {17,  0,0x65,0x18,0x04,0x03,0x23,0x70,0x03,0x18,0x03,0x03,0x24,0x70,0x03,0x10,0x73,0x10,0x73}},
	{LCM_SEND(19), {17,  0,0x66,0x18,0x02,0x03,0x25,0x70,0x03,0x18,0x01,0x03,0x26,0x70,0x03,0x10,0x73,0x10,0x73}},
	{LCM_SEND(19), {17,  0,0x67,0x18,0x00,0x03,0x27,0x70,0x03,0x10,0x01,0x03,0x28,0x70,0x03,0x10,0x73,0x10,0x73}},
	{LCM_SEND(16), {14,  0,0x68,0x00,0x08,0x0A,0x08,0x09,0x00,0x00,0x08,0x0A,0x08,0x09,0x00,0x00}},
	{LCM_SEND(11), {9,  0,0x60,0x18,0x08,0x10,0x73,0x18,0x07,0x10,0x73}},
	{LCM_SEND(11), {9,  0,0x61,0x18,0x00,0x68,0x68,0x18,0x00,0x68,0x68}},
	{LCM_SEND(11), {9,  0,0x62,0x18,0x00,0x68,0x68,0x18,0x00,0x68,0x68}},
	{LCM_SEND(11), {9,  0,0x63,0x18,0x00,0x68,0x68,0x18,0x08,0x10,0x73}},
	{LCM_SEND(11), {9,  0,0x6A,0x08,0x0D,0x88,0x0A,0x01,0x23,0x45,0x67}},
	{LCM_SEND(10), {8,  0,0x69,0x14,0x22,0x14,0x22,0x14,0x22,0x08}},
	{LCM_SEND(2), {0x6B,0xA7}},
//endgatetiming
//gamma
	{LCM_SEND(55), {53,  0,0xD1,0x00,0x00,0x00,0x03,0x00,0x1A,0x00,0x31,0x00,0x45,0x00,0x82,0x00,0xC0,0x01,0x07,0x01,0x3D,0x01,0x8F,0x01,0xC5,0x02,0x1D,0x02,0x58,0x02,0x5A,0x02,0x91,0x02,0xC8,0x02,0xE7,0x03,0x0C,0x03,0x22,0x03,0x3F,0x03,0x55,0x03,0x75,0x03,0x80,0x03,0x95,0x03,0xC2,0x03,0xFF}},
	{LCM_SEND(55), {53,  0,0xD2,0x00,0x00,0x00,0x03,0x00,0x1A,0x00,0x31,0x00,0x45,0x00,0x82,0x00,0xC0,0x01,0x07,0x01,0x3D,0x01,0x8F,0x01,0xC5,0x02,0x1D,0x02,0x58,0x02,0x5A,0x02,0x91,0x02,0xC8,0x02,0xE7,0x03,0x0C,0x03,0x22,0x03,0x3F,0x03,0x55,0x03,0x75,0x03,0x80,0x03,0x95,0x03,0xC2,0x03,0xFF}},
	{LCM_SEND(55), {53,  0,0xD3,0x00,0x00,0x00,0x03,0x00,0x1A,0x00,0x31,0x00,0x45,0x00,0x82,0x00,0xC0,0x01,0x07,0x01,0x3D,0x01,0x8F,0x01,0xC5,0x02,0x1D,0x02,0x58,0x02,0x5A,0x02,0x91,0x02,0xC8,0x02,0xE7,0x03,0x0C,0x03,0x22,0x03,0x3F,0x03,0x55,0x03,0x75,0x03,0x80,0x03,0x95,0x03,0xC2,0x03,0xFF}},
	{LCM_SEND(55), {53,  0,0xD4,0x00,0x00,0x00,0x03,0x00,0x1A,0x00,0x31,0x00,0x45,0x00,0x82,0x00,0xC0,0x01,0x07,0x01,0x3D,0x01,0x8F,0x01,0xC5,0x02,0x1D,0x02,0x58,0x02,0x5A,0x02,0x91,0x02,0xC8,0x02,0xE7,0x03,0x0C,0x03,0x22,0x03,0x3F,0x03,0x55,0x03,0x75,0x03,0x80,0x03,0x95,0x03,0xC2,0x03,0xFF}},
	{LCM_SEND(55), {53,  0,0xD5,0x00,0x00,0x00,0x03,0x00,0x1A,0x00,0x31,0x00,0x45,0x00,0x82,0x00,0xC0,0x01,0x07,0x01,0x3D,0x01,0x8F,0x01,0xC5,0x02,0x1D,0x02,0x58,0x02,0x5A,0x02,0x91,0x02,0xC8,0x02,0xE7,0x03,0x0C,0x03,0x22,0x03,0x3F,0x03,0x55,0x03,0x75,0x03,0x80,0x03,0x95,0x03,0xC2,0x03,0xFF}},
	{LCM_SEND(55), {53,  0,0xD6,0x00,0x00,0x00,0x03,0x00,0x1A,0x00,0x31,0x00,0x45,0x00,0x82,0x00,0xC0,0x01,0x07,0x01,0x3D,0x01,0x8F,0x01,0xC5,0x02,0x1D,0x02,0x58,0x02,0x5A,0x02,0x91,0x02,0xC8,0x02,0xE7,0x03,0x0C,0x03,0x22,0x03,0x3F,0x03,0x55,0x03,0x75,0x03,0x80,0x03,0x95,0x03,0xC2,0x03,0xFF}},

	{LCM_SEND(1), {0x11}},
	{LCM_SLEEP(120)},
	{LCM_SEND(1), {0x29}},
	{LCM_SLEEP(100)},
};

static LCM_Force_Cmd_Code rd_prep_code[]={
	{0x39, {LCM_SEND(6), {4,0,0xBF,0x91,0x61,0xF2}}},
	{0x37, {LCM_SEND(2), {0x3, 0}}},
};

static LCM_Force_Cmd_Code rd_prep_code_1[]={
	{0x37, {LCM_SEND(2), {0x3, 0}}},
};
static LCM_Init_Code disp_on[] ={{LCM_SEND(1), {0x29}}};

static LCM_Init_Code sleep_in[] =  {
	{LCM_SEND(8), {6,0,0xF0, 0x55, 0xAA, 0x52 ,0x08, 0x00}},
	{LCM_SEND(2), {0xa9,0xf0}},
	{LCM_SEND(2), {0x77,0x29}},
	{LCM_SEND(5), {3,  0,0x6c,0x00,0x21}},
	{LCM_SLEEP(20)},
	{LCM_SEND(6), {4,  0,0x74,0x00,0x7f,0x00}},
	{LCM_SEND(6), {4, 0, 0x75, 0x42, 0x02, 0x24}},
	{LCM_SLEEP(20)},
	{LCM_SEND(5), {3,  0,0x6c,0x00,0x22}},
	{LCM_SLEEP(20)},
	{LCM_SEND(2), {0x28,0x00}},
	{LCM_SLEEP(10)},
	{LCM_SEND(2), {0x10,0x00}},
	{LCM_SLEEP(200)},
	{LCM_SEND(8), {6,0,0xF0, 0x55, 0xAA, 0x52, 0x08, 0x01}},
	{LCM_SEND(2), {0xa8,0xa0}},
	{LCM_SEND(5), {3,  0,0xF7,0x1c,0x82}},
};
static LCM_Init_Code sleep_out[] =  {
	{LCM_SEND(1), {0x11}},
	{LCM_SLEEP(120)},
	{LCM_SEND(1), {0x29}},
	{LCM_SLEEP(20)},
};
static int32_t gc9503_mipi_init(struct panel_spec *self)
{
	int32_t i;
	LCM_Init_Code *init = init_data;
	unsigned int tag;

	mipi_set_cmd_mode_t mipi_set_cmd_mode = self->info.mipi->ops->mipi_set_cmd_mode;
	mipi_gen_write_t mipi_gen_write = self->info.mipi->ops->mipi_gen_write;
	mipi_eotp_set_t mipi_eotp_set = self->info.mipi->ops->mipi_eotp_set;
	LCD_PRINT("uboot lcd_gc9503_init\n");

	mipi_set_cmd_mode();

	mdelay(5);
	mipi_eotp_set(1,0);
	for(i = 0; i < ARRAY_SIZE(init_data); i++){
		tag = (init->tag >>24);
		if(tag & LCM_TAG_SEND){
			mipi_gen_write(init->data, (init->tag & LCM_TAG_MASK));
			udelay(20);
		}else if(tag & LCM_TAG_SLEEP){
			mdelay((init->tag & LCM_TAG_MASK));
		}
		init++;
	}

	mdelay(5);
	mipi_eotp_set(1,1);
	return 0;
}

static uint32_t gc9503_readid(struct panel_spec *self)
{
	/* TODO: need read id*/
	int32_t i = 0;
	int32_t j =0;
	LCM_Force_Cmd_Code * rd_prepare = rd_prep_code;
	uint8_t read_data[3] = {0};
	int32_t read_rtn = 0;
	unsigned int tag = 0;
	mipi_set_cmd_mode_t mipi_set_cmd_mode = self->info.mipi->ops->mipi_set_cmd_mode;
	mipi_force_write_t mipi_force_write = self->info.mipi->ops->mipi_force_write;
	mipi_force_read_t mipi_force_read = self->info.mipi->ops->mipi_force_read;
	mipi_eotp_set_t mipi_eotp_set = self->info.mipi->ops->mipi_eotp_set;
	LCD_PRINT("uboot lcd_gc9503_mipi read id!\n");

	mipi_set_cmd_mode();
	mipi_eotp_set(1,0);

	for(j = 0; j < 4; j++){
		rd_prepare = rd_prep_code;
		for(i = 0; i < ARRAY_SIZE(rd_prep_code); i++){
			tag = (rd_prepare->real_cmd_code.tag >> 24);
			if(tag & LCM_TAG_SEND){
				mipi_force_write(rd_prepare->datatype, rd_prepare->real_cmd_code.data, (rd_prepare->real_cmd_code.tag & LCM_TAG_MASK));
				udelay(20);
			}else if(tag & LCM_TAG_SLEEP){
				mdelay((rd_prepare->real_cmd_code.tag & LCM_TAG_MASK));
			}
			rd_prepare++;
		}
		mdelay(50);
		read_rtn = mipi_force_read(0x04, 3,(uint8_t *)read_data);
		LCD_PRINT("lcd_gc9503_mipi read id 0x04 value is 0x%x,0x%x,0x%x!\n", read_data[0],read_data[1],read_data[2]);

		if((0x95 == read_data[1]) && ((0x03 == read_data[2]) || (0x04 == read_data[2]))){
			LCD_PRINT("lcd_gc9503_mipi read id success!\n");
			return 0x9503;
		}
	}

	mdelay(5);
	mipi_eotp_set(1,1);

	return 0x0;
}

int32_t gc9503_reset(struct panel_spec *self)
{
	sprd_gpio_request(NULL, LCM_GPIO_RSTN);
	sprd_gpio_direction_output(NULL, LCM_GPIO_RSTN, 1);
	mdelay(5);
	sprd_gpio_direction_output(NULL, LCM_GPIO_RSTN, 0);
	mdelay(5);
	sprd_gpio_direction_output(NULL, LCM_GPIO_RSTN, 1);
	mdelay(20);

	return 0;
}

static struct panel_operations lcd_gc9503_mipi_operations = {
	.panel_init = gc9503_mipi_init,
	.panel_readid = gc9503_readid,
	.panel_reset = gc9503_reset,
};

static struct timing_rgb lcd_gc9503_mipi_timing = {
#if 0
	.hfp = 10, /* unit: pixel */
	.hbp = 10,
	.hsync = 10,
	.vfp = 6, //9, /*unit: line */
	.vbp = 8,  //5,
	.vsync = 4,
#else
	.hfp = 20, /* unit: pixel */
	.hbp = 20,
	.hsync = 12,
	.vfp = 14, //9, /*unit: line */
	.vbp = 12,  //5,
	.vsync = 8,
#endif
};

static struct info_mipi lcd_gc9503_mipi_info = {
	.work_mode  = SPRDFB_MIPI_MODE_VIDEO,
	.video_bus_width = 24, /*18,16*/
	.lan_number = 2,
	.phy_feq = 480*1000,
	.h_sync_pol = SPRDFB_POLARITY_POS,
	.v_sync_pol = SPRDFB_POLARITY_POS,
	.de_pol = SPRDFB_POLARITY_POS,
	.te_pol = SPRDFB_POLARITY_POS,
	.color_mode_pol = SPRDFB_POLARITY_NEG,
	.shut_down_pol = SPRDFB_POLARITY_NEG,
	.timing = &lcd_gc9503_mipi_timing,
	.ops = NULL,
};

struct panel_spec lcd_gc9503_jl_mipi_spec = {
	.width = 480,
	.height = 800,
	.fps = 60,
	.type = LCD_MODE_DSI,
	.direction = LCD_DIRECT_NORMAL,
	.is_need_reset = 1,
	.info = {
		.mipi = &lcd_gc9503_mipi_info
	},
	.ops = &lcd_gc9503_mipi_operations,
};


